package com.douqu.game.core.config.sprite;

import com.douqu.game.core.config.ProfessionConfig;
import com.douqu.game.core.config.battle.SkillConfig;
import com.douqu.game.core.config.card.CardConfig;
import com.douqu.game.core.config.card.CardStarAttributeConfig;
import com.douqu.game.core.config.common.CommonData;
import com.douqu.game.core.e.E_Attribute;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.util.CoreUtils;

/**
 * Created by bean on 2017/7/14.
 */
public class SoldierConfig extends Sprite {

    //客户端用的
    public String birthSkillDes, birthSkillName, birthSkillIcon, starSkills;

    /** 释放范围(召唤出来的时候最远能放到几格) */
    public int initRange;

    /** AI */
    public int ai;

    /** 视野(在视野范围内就要被吸引过去攻击) */
    public int vision;

    /** 技能列表 */
    public SoldierStarSkill[] skills;

    /** 升星增加攻击
     * 索引 对应星级
     * */
    public int[] starAtk;

    /** 升星增加防御
     * 索引 对应星级
     * */
    public int[] starDef;

    /** 升星增加生命
     * 索引 对应星级
     * */
    public int[] starHp;

    /**
     * 升星属性
     * id:星级
     * value:对应CardStarAttribute的ID
     */
    public CommonData[] starAttribute;


    /**
     * 品质,从卡牌里面继承而来
     */
    private CardConfig cardConfig;

    /**饰品*/
    public int [] accessory;


    @Override
    public boolean check()
    {
        if(skills == null)
        {
            LogUtils.error("Load Soldier skills is null -> id:" + id);
            return false;
        }
        if(starSkills == null)
        {
            LogUtils.error("Load Soldier starSkills is null -> id:" + id);
            return false;
        }
        if(starHp == null)
        {
            LogUtils.error("Load Soldier starHp is null -> id:" + id);
            return false;
        }
        if(starDef == null)
        {
            LogUtils.error("Load Soldier starDef is null -> id:" + id);
            return false;
        }
        if(starAtk == null)
        {
            LogUtils.error("Load Soldier starAtk is null -> id:" + id);
            return false;
        }
        if(accessory == null)
        {
            LogUtils.error("Load Soldier accessory is null -> id:" + id);
            return false;
        }

        if(gridArea > 3)
        {
            LogUtils.error("Soldier gridArea(占位太大,请检查) -> id:" + id);
            return false;
        }

        return super.check();
    }

    /**
     * 获取星级属性
     * @param attrId
     * @param star
     * @return
     */
    @Override
    public int getStarAttribute(int attrId, int star)
    {
        int[] data = null;
        if(attrId == E_Attribute.ATK.getCode())
            data = starAtk;
        else if(attrId == E_Attribute.DEF.getCode())
            data = starDef;
        else if(attrId == E_Attribute.HP.getCode())
            data = starHp;

        int result = 0;
        if(data != null && star >= 0 && star < data.length)
            result = data[star];

        for(CommonData commonData : starAttribute)
        {
            if(star >= commonData.id)
            {
                CardStarAttributeConfig cardStarAttributeConfig = DataFactory.getInstance().getGameObject(DataFactory.CARD_STAR_ATTRIBUTE_KEY, commonData.value);
                result += cardStarAttributeConfig.getAttribute(attrId);
            }
        }

        return result;
    }


    /**
     * 获取普通攻击
     * @param star
     * @return
     */
    public SkillConfig getDefaultSkill(int star)
    {
        for(int i = skills.length - 1; i >= 0; i--)
        {
            if(star >= skills[i].star)
                return skills[i].defaultSkill;
        }

        return null;
    }

    /**
     * 获取星级技能
     * @param star
     * @return
     */
    public SkillConfig[] getStarSkills(int star)
    {
        for(int i = skills.length - 1; i >= 0; i--)
        {
            if(star >= skills[i].star)
                return skills[i].skills;
        }

        return null;
    }

    /**
     * 是否能克制对方
     * @param target
     * @return
     */
    @Override
    public boolean isRestrict(Sprite target)
    {
        if(isSameRestrict(target))
            return false;

        if(target instanceof SoldierConfig)
        {
            ProfessionConfig pro = DataFactory.getInstance().getGameObject(DataFactory.PROFESSION_KEY, profession);
            return pro.restrictType == ((SoldierConfig) target).profession;
        }

        return false;
    }

    /**
     * 是否跟对方克制属性一样
     * @param target
     * @return
     */
    @Override
    public boolean isSameRestrict(Sprite target)
    {
        return profession == target.profession;
    }





    @Override
    public boolean setVariable(String key, String value)
    {
        if("skills".equals(key))
        {
            if(CoreUtils.isErrorValue(value))
            {
                LogUtils.error("Load Soldier skills is null, id: " + id);
                return false;
            }
            else
            {
                String[] strs = value.split(ConstantFactory.DIVISION);
                skills = new SoldierStarSkill[strs.length];
                for(int i = 0; i < strs.length; i++)
                {
                    skills[i] = new SoldierStarSkill(id, strs[i]);
                    if(skills[i].isLoadError())
                        return false;
                }
            }
            return true;
        }
        else if("starAtk".equals(key))
        {
            starAtk = LoadUtils.loadIntArray(key, value);
            return true;
        }
        else if("starDef".equals(key))
        {
            starDef = LoadUtils.loadIntArray(key, value);
            return true;
        }
        else if("starHp".equals(key))
        {
            starHp = LoadUtils.loadIntArray(key, value);
            return true;
        }
        else if("starAttribute".equals(key))
        {
            starAttribute = LoadUtils.loadCommonDataToArray(key, value);
            for(CommonData commonData : starAttribute)
            {
                if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.CARD_STAR_ATTRIBUTE_KEY, commonData.value) == null)
                {
                    LogUtils.error("Load Soldier starAttribute Error -> id:" + id + " value:" + commonData.value);
                    return false;
                }
            }
            return true;
        }
        else if("accessory".equals(key)) {
            if(CoreUtils.isErrorValue(value))
                accessory = new int[0];
            else
            {
                accessory = LoadUtils.loadIntArray(key, value);
                for(Integer accessoryId : accessory)
                {
                    if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.ACCESSORY_KEY, accessoryId) == null)
                    {
                        LogUtils.error("Load Soldier accessory Error -> id:" + id + "  accessoryId:" + accessoryId);
                        return false;
                    }
                }
            }
            return true;
        }
        else
            return super.setVariable(key, value);
    }





    public CardConfig getCardConfig() {
        return cardConfig;
    }

    public void setCardConfig(CardConfig cardConfig) {
        this.cardConfig = cardConfig;
    }





}
